<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.2.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/murmur/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/murmur/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/murmur/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/murmur/images/logo.svg" color="#222">

<link rel="stylesheet" href="/murmur/css/main.css">



<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.1/css/all.min.css">
  <link rel="stylesheet" href="//cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css">
  <link rel="stylesheet" href="//cdn.jsdelivr.net/npm/nprogress@0.2.0/nprogress.css">
  <script src="//cdn.jsdelivr.net/npm/nprogress@0.2.0/nprogress.js"></script>

<script class="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"gitee.com","root":"/murmur/","scheme":"Gemini","version":"8.0.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":true,"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":"disqus","storage":true,"lazyload":false,"nav":null},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"}};
  </script>

  <meta name="description" content="第6章： 行为委托 先简单回顾一下JavaScript的[[Prototype]]机制： JavaScript的[[Prototype]机制，本质上就是对象之间的关联关系。 [[Prototype]]机制是指对象内部中包含另一个对象的引用，当第一个对象的属性访问不到时，引擎会通过引用，到[[Prototype]]关联的对象上继续查找，后者也没有找到，就会查找它的[[Prototype]]，以此类推">
<meta property="og:type" content="article">
<meta property="og:title" content="《你不知道的JavaScript(上)》S02E06">
<meta property="og:url" content="https://gitee.com/lianghuilin/murmur.git/reading_notes/%E3%80%8A%E4%BD%A0%E4%B8%8D%E7%9F%A5%E9%81%93%E7%9A%84JavaScript-%E4%B8%8A-%E3%80%8BS02E06/index.html">
<meta property="og:site_name" content="汇话板">
<meta property="og:description" content="第6章： 行为委托 先简单回顾一下JavaScript的[[Prototype]]机制： JavaScript的[[Prototype]机制，本质上就是对象之间的关联关系。 [[Prototype]]机制是指对象内部中包含另一个对象的引用，当第一个对象的属性访问不到时，引擎会通过引用，到[[Prototype]]关联的对象上继续查找，后者也没有找到，就会查找它的[[Prototype]]，以此类推">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2020-12-01T14:31:05.000Z">
<meta property="article:modified_time" content="2020-12-01T15:07:24.558Z">
<meta property="article:author" content="William Leung">
<meta property="article:tag" content="你不知道的JavaScript">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="https://gitee.com/lianghuilin/murmur.git/reading_notes/%E3%80%8A%E4%BD%A0%E4%B8%8D%E7%9F%A5%E9%81%93%E7%9A%84JavaScript-%E4%B8%8A-%E3%80%8BS02E06/">


<script class="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>《你不知道的JavaScript(上)》S02E06 | 汇话板</title>
  


  <script>
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?a49081c570a782a242cdf6e19e1a584d";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>




  <noscript>
  <style>
  body { margin-top: 2rem; }

  .use-motion .menu-item,
  .use-motion .sidebar,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header {
    visibility: visible;
  }

  .use-motion .header,
  .use-motion .site-brand-container .toggle,
  .use-motion .footer { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle,
  .use-motion .custom-logo-image {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line {
    transform: scaleX(1);
  }

  .search-pop-overlay, .sidebar-nav { display: none; }
  .sidebar-panel { display: block; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/murmur/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">汇话板</h1>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">设定目标，放慢步调，用温和的力量自我改革</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/murmur/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/murmur/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
  </ul>
</nav>




</div>
        
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <section class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E7%AC%AC6%E7%AB%A0%EF%BC%9A-%E8%A1%8C%E4%B8%BA%E5%A7%94%E6%89%98"><span class="nav-number">1.</span> <span class="nav-text">第6章： 行为委托</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#6-1-%E9%9D%A2%E5%90%91%E5%A7%94%E6%89%98%E7%9A%84%E8%AE%BE%E8%AE%A1"><span class="nav-number">1.0.1.</span> <span class="nav-text">6.1 面向委托的设计</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#6-1-1-%E7%B1%BB%E7%90%86%E8%AE%BA"><span class="nav-number">1.0.1.0.1.</span> <span class="nav-text">6.1.1 类理论</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#6-1-2-%E5%A7%94%E6%89%98%E7%90%86%E8%AE%BA"><span class="nav-number">1.0.1.0.2.</span> <span class="nav-text">6.1.2 委托理论</span></a><ol class="nav-child"><li class="nav-item nav-level-6"><a class="nav-link" href="#%E2%80%9C%E9%9D%A2%E5%90%91%E7%B1%BB%E2%80%9D%E5%92%8C%E2%80%9C%E9%9D%A2%E5%90%91%E5%A7%94%E6%89%98%E2%80%9D%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F%E7%9A%84%E5%8C%BA%E5%88%AB"><span class="nav-number">1.0.1.0.2.1.</span> <span class="nav-text">“面向类”和“面向委托”设计模式的区别</span></a></li></ol></li><li class="nav-item nav-level-5"><a class="nav-link" href="#6-1-3-%E6%AF%94%E8%BE%83%E6%80%9D%E7%BB%B4%E6%A8%A1%E5%9E%8B"><span class="nav-number">1.0.1.0.3.</span> <span class="nav-text">6.1.3 比较思维模型</span></a></li></ol></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#6-2-%E7%B1%BB%E4%B8%8E%E5%AF%B9%E8%B1%A1"><span class="nav-number">1.0.2.</span> <span class="nav-text">6.2 类与对象</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#6-3-%E6%9B%B4%E7%AE%80%E6%B4%81%E7%9A%84%E8%AE%BE%E8%AE%A1"><span class="nav-number">1.0.3.</span> <span class="nav-text">6.3 更简洁的设计</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#6-4-%E6%9B%B4%E5%A5%BD%E7%9A%84%E8%AF%AD%E6%B3%95"><span class="nav-number">1.0.4.</span> <span class="nav-text">6.4 更好的语法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#6-5-%E5%86%85%E7%9C%81"><span class="nav-number">1.0.5.</span> <span class="nav-text">6.5 内省</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#6-6-%E5%B0%8F%E7%BB%93"><span class="nav-number">1.0.6.</span> <span class="nav-text">6.6 小结</span></a></li></ol></li></ol></li></ol></div>
        </section>
        <!--/noindex-->

        <section class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">William Leung</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/murmur/archives">
          <span class="site-state-item-count">43</span>
          <span class="site-state-item-name">文章</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/murmur/categories/">
          
        <span class="site-state-item-count">9</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/murmur/tags/">
        <span class="site-state-item-count">1</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



        </section>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">
      

      

  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://gitee.com/lianghuilin/murmur.git/reading_notes/%E3%80%8A%E4%BD%A0%E4%B8%8D%E7%9F%A5%E9%81%93%E7%9A%84JavaScript-%E4%B8%8A-%E3%80%8BS02E06/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/murmur/images/avatar.gif">
      <meta itemprop="name" content="William Leung">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="汇话板">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          《你不知道的JavaScript(上)》S02E06
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>
      

      <time title="创建时间：2020-12-01 22:31:05 / 修改时间：23:07:24" itemprop="dateCreated datePublished" datetime="2020-12-01T22:31:05+08:00">2020-12-01</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/murmur/categories/%E4%B8%80%E5%91%A8%E4%B8%80%E7%AB%A0%E5%89%8D%E7%AB%AF%E4%B9%A6/" itemprop="url" rel="index"><span itemprop="name">一周一章前端书</span></a>
        </span>
    </span>

  
    <span class="post-meta-item" title="本文字数">
      <span class="post-meta-item-icon">
        <i class="far fa-file-word"></i>
      </span>
      <span class="post-meta-item-text">本文字数：</span>
      <span>12k</span>
    </span>
    <span class="post-meta-item" title="阅读时长">
      <span class="post-meta-item-icon">
        <i class="far fa-clock"></i>
      </span>
      <span class="post-meta-item-text">阅读时长 &asymp;</span>
      <span>11 分钟</span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h1 id="第6章：-行为委托"><a href="#第6章：-行为委托" class="headerlink" title="第6章： 行为委托"></a>第6章： 行为委托</h1><ul>
<li>先简单回顾一下JavaScript的<code>[[Prototype]]</code>机制：<ul>
<li>JavaScript的<code>[[Prototype]</code>机制，本质上就是对象之间的关联关系。</li>
<li><code>[[Prototype]]</code>机制是指对象内部中包含另一个对象的引用，当第一个对象的属性访问不到时，引擎会通过引用，到<code>[[Prototype]]</code>关联的对象上继续查找，后者也没有找到，就会查找它的<code>[[Prototype]]</code>，以此类推，这一系列的对象被称为<code>原型链</code>。</li>
</ul>
</li>
<li>我们也说过，与其将JavaScript术语<code>[[Prototype]]</code>称为 <strong>原型继承</strong> ，不如叫 <strong>原型委托</strong> 更为准确。</li>
<li>而本章主要讲解就是“面向类”和“面向委托”设计模式之间的区别。</li>
</ul>
<h3 id="6-1-面向委托的设计"><a href="#6-1-面向委托的设计" class="headerlink" title="6.1 面向委托的设计"></a>6.1 面向委托的设计</h3><h5 id="6-1-1-类理论"><a href="#6-1-1-类理论" class="headerlink" title="6.1.1 类理论"></a>6.1.1 类理论</h5><ul>
<li>面向类的设计模式，通常先“抽象”父类的特征，然后用子类继承父类后进行特殊化。</li>
<li>举例说，用面向类的设计模式实现“汽车”和“飞机”：<ul>
<li>定义一个通用的父类<code>Transport</code>(运输工具)，Transport类定义公共的特性和行为；</li>
<li>接着定义子类<code>Car</code>(汽车)和<code>Aircraft</code>(飞机)，继承自<code>Transport</code>并且对自身的属性和行为进行特殊化；<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Transport</span> </span>&#123;</span><br><span class="line">    <span class="comment">//构造函数</span></span><br><span class="line">    Transport(id,passengerNum,name);  </span><br><span class="line">    </span><br><span class="line">    id;</span><br><span class="line">    passengerNum;   <span class="comment">//乘客数</span></span><br><span class="line">    name;         <span class="comment">//品牌名字</span></span><br><span class="line">    <span class="comment">//启动</span></span><br><span class="line">    <span class="function"><span class="title">launch</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">&#x27;载重人数：&#x27;</span>+passengerNum,<span class="string">&#x27;品牌名字：&#x27;</span>+name);</span><br><span class="line">    &#125;;    </span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Car</span> <span class="title">inherits</span> <span class="title">Transport</span></span>&#123;</span><br><span class="line">    <span class="comment">//构造函数</span></span><br><span class="line">    <span class="function"><span class="title">Car</span>(<span class="params">id,passengerNum,name,wheelNum</span>)</span>&#123;</span><br><span class="line">        <span class="built_in">super</span>(id,passengerNum,name);</span><br><span class="line">        wheelNum = wheelNum;    </span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    wheelNum;  <span class="comment">//轮子数量</span></span><br><span class="line">    <span class="function"><span class="title">launch</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="built_in">super</span>();</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">&#x27;轮子数量：&#x27;</span> + wheelNum);</span><br><span class="line">    &#125;;   </span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Aircraft</span> <span class="title">inherits</span> <span class="title">Transport</span></span>&#123;</span><br><span class="line">    <span class="comment">//构造函数</span></span><br><span class="line">    <span class="function"><span class="title">Aircraft</span>(<span class="params">id,passengerNum,name,wingNum</span>)</span>&#123;</span><br><span class="line">        <span class="built_in">super</span>(id,passengerNum,name);</span><br><span class="line">        wingNum = wingNum;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    wingNum;    <span class="comment">//机翼数量</span></span><br><span class="line">    <span class="function"><span class="title">launch</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="built_in">super</span>();</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">&#x27;机翼数量：&#x27;</span> + wingNum);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h5 id="6-1-2-委托理论"><a href="#6-1-2-委托理论" class="headerlink" title="6.1.2 委托理论"></a>6.1.2 委托理论</h5></li>
</ul>
</li>
<li>如果用面向委托的设计模式考虑同样的问题呢：<ul>
<li>同样需要定义<code>Transport</code>(运输工具)，但它只是包含公共的功能方法；</li>
<li>接着定义<code>Car</code>(汽车)和<code>Aircraft</code>(飞机)，存储具体的数据以及特殊方法；<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Transport</span> </span>&#123;</span><br><span class="line">    setId : <span class="function"><span class="keyword">function</span>(<span class="params">id</span>)</span>&#123;</span><br><span class="line">        <span class="built_in">this</span>.id = id;</span><br><span class="line">    &#125;,</span><br><span class="line">    setPassengerNum : <span class="function"><span class="keyword">function</span>(<span class="params">num</span>)</span>&#123;</span><br><span class="line">        <span class="built_in">this</span>.passengerNum = num;</span><br><span class="line">    &#125;,</span><br><span class="line">    setName : <span class="function"><span class="keyword">function</span>(<span class="params">name</span>)</span>&#123;</span><br><span class="line">        <span class="built_in">this</span>.name = name;</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="function"><span class="title">launch</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">&#x27;载重人数：&#x27;</span>+<span class="built_in">this</span>.passengerNum,<span class="string">&#x27;品牌名字：&#x27;</span>+<span class="built_in">this</span>.name);</span><br><span class="line">    &#125;,    </span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//Car委托Transport</span></span><br><span class="line">Car = <span class="built_in">Object</span>.create(Transport);</span><br><span class="line">Car.prepareTransport = <span class="function"><span class="keyword">function</span>(<span class="params">id,passengerNum,name,wheelNum</span>)</span>&#123;</span><br><span class="line">    <span class="built_in">this</span>.setId(id);</span><br><span class="line">    <span class="built_in">this</span>.setPassengerNum(passengerNum);</span><br><span class="line">    <span class="built_in">this</span>.wheelNum = wheelNum;</span><br><span class="line">&#125;</span><br><span class="line">Car.prepareLaunch = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="built_in">this</span>.launch();</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&#x27;轮子数量：&#x27;</span> + wheelNum);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

</li>
</ul>
</li>
</ul>
<h6 id="“面向类”和“面向委托”设计模式的区别"><a href="#“面向类”和“面向委托”设计模式的区别" class="headerlink" title="“面向类”和“面向委托”设计模式的区别"></a>“面向类”和“面向委托”设计模式的区别</h6><ul>
<li>数据存储在委托者(<code>Car</code>/<code>Aircraft</code>)上，而不是委托对象(<code>Transport</code>)上；</li>
<li>在面向类的设计模式中，父类和子类都拥有同名的方法<code>launch()</code>，但在面向委托的设计模式中，我们会尽量避免在<code>[[Prototype]]</code>链条的不同级别中，使用相同的命名；</li>
<li>如同<code>this.setId()</code>方法，由于调用位置触发了this的隐式绑定规则，虽然<code>setId()</code>方法在<code>Transport</code>中，运行时，仍然会绑定到<code>Car</code>。这说明，委托行为意味着<code>Car</code>对象在找不到属性或者方法引用时，会把这个请求委托给另一个对象<code>Transport</code>。</li>
</ul>
<blockquote>
<p>注意：在两个或两个以上互相委托的对象之间，创建循环委托是禁止的。 </p>
</blockquote>
<h5 id="6-1-3-比较思维模型"><a href="#6-1-3-比较思维模型" class="headerlink" title="6.1.3 比较思维模型"></a>6.1.3 比较思维模型</h5><ul>
<li>用伪代码，从理论上了解“面向类”和“面向委托”两种设计模式的区别后，我们从具体的JavaScript代码来比较两种设计模式的异同：<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 交通工具</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param <span class="type">&#123;*&#125;</span> </span>id </span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Transport</span>(<span class="params">id,name,passengerNum</span>)</span>&#123;</span><br><span class="line">    <span class="built_in">this</span>.id = id;</span><br><span class="line">    <span class="built_in">this</span>.name = name;</span><br><span class="line">    <span class="built_in">this</span>.passengerNum = passengerNum;</span><br><span class="line">&#125;</span><br><span class="line">Transport.prototype.launch = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&#x27;载重人数：&#x27;</span>+<span class="built_in">this</span>.passengerNum,<span class="string">&#x27;品牌名字：&#x27;</span>+<span class="built_in">this</span>.name);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 汽车</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param <span class="type">&#123;*&#125;</span> </span>id </span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param <span class="type">&#123;*&#125;</span> </span>name </span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param <span class="type">&#123;*&#125;</span> </span>passengerNum </span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Car</span>(<span class="params">id,name,passengerNum</span>)</span>&#123;</span><br><span class="line">    Transport.call(<span class="built_in">this</span>,id,name,passengerNum);</span><br><span class="line">&#125;</span><br><span class="line">Car.prototype = <span class="built_in">Object</span>.create(Transport.prototype);</span><br><span class="line">Car.prototype.run = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="built_in">this</span>.launch();</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//实例化</span></span><br><span class="line"><span class="keyword">var</span> lexus = <span class="keyword">new</span> Car(<span class="number">1</span>,<span class="string">&#x27;雷克萨斯&#x27;</span>,<span class="number">8</span>);</span><br><span class="line"><span class="keyword">var</span> bmw = <span class="keyword">new</span> Car(<span class="number">2</span>,<span class="string">&#x27;宝马&#x27;</span>,<span class="number">8</span>);</span><br><span class="line"></span><br><span class="line">lexus.run();</span><br><span class="line">bmw.run();</span><br></pre></td></tr></table></figure></li>
<li>再用“面向委托”的设计模式，通过对象关联的代码风格来编写同样的代码：<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 交通工具</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line">Transport = &#123;</span><br><span class="line">    init : <span class="function"><span class="keyword">function</span>(<span class="params">id,name,passengerNum</span>)</span>&#123;</span><br><span class="line">        <span class="built_in">this</span>.id = id;</span><br><span class="line">        <span class="built_in">this</span>.name = name;</span><br><span class="line">        <span class="built_in">this</span>.passengerNum = passengerNum;</span><br><span class="line">    &#125;,</span><br><span class="line">    launch : <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">&#x27;载重人数：&#x27;</span>+<span class="built_in">this</span>.passengerNum,<span class="string">&#x27;品牌名字：&#x27;</span>+<span class="built_in">this</span>.name);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 汽车</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line">Car = <span class="built_in">Object</span>.create(Transport);</span><br><span class="line">Car.run = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="built_in">this</span>.launch();</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">//实例化</span></span><br><span class="line"><span class="keyword">var</span> mazda = <span class="built_in">Object</span>(Car);</span><br><span class="line">mazda.init(<span class="number">1</span>,<span class="string">&#x27;马自达&#x27;</span>,<span class="number">4</span>);</span><br><span class="line">mazda.run();</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> toyota = <span class="built_in">Object</span>(Car);</span><br><span class="line">toyota.init(<span class="number">2</span>,<span class="string">&#x27;丰田&#x27;</span>,<span class="number">4</span>);</span><br><span class="line">toyota.run();</span><br></pre></td></tr></table></figure></li>
<li>对比发现，代码简洁很多。很多时候我们要的只是将对象关联起来，并不需要那些既复杂又令人困惑的模仿类的行为，比如构造函数、原型以及<code>new</code>。</li>
</ul>
<h3 id="6-2-类与对象"><a href="#6-2-类与对象" class="headerlink" title="6.2 类与对象"></a>6.2 类与对象</h3><ul>
<li>接着我们通过真实的web前端案例来感受两种设计模式的不同，比如说我们要在页面上创建按钮控件。<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Widget父类</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param <span class="type">&#123;*&#125;</span> </span>width </span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param <span class="type">&#123;*&#125;</span> </span>height </span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Widget</span>(<span class="params">width,height</span>)</span>&#123;</span><br><span class="line">    <span class="built_in">this</span>.width = width;</span><br><span class="line">    <span class="built_in">this</span>.height = height;</span><br><span class="line">    <span class="built_in">this</span>.$elem = <span class="literal">null</span>;</span><br><span class="line">&#125;</span><br><span class="line">Widget.prototype.render = <span class="function"><span class="keyword">function</span>(<span class="params">$where</span>)</span>&#123;</span><br><span class="line">    <span class="keyword">if</span>(<span class="built_in">this</span>.$elem)&#123;</span><br><span class="line">        <span class="built_in">this</span>.$elem.css(&#123;</span><br><span class="line">            width : <span class="built_in">this</span>.width + <span class="string">&#x27;px&#x27;</span>,</span><br><span class="line">            height: <span class="built_in">this</span>.height + <span class="string">&#x27;px&#x27;</span></span><br><span class="line">        &#125;).appendTo($where);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Button子类</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param <span class="type">&#123;*&#125;</span> </span>width </span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param <span class="type">&#123;*&#125;</span> </span>height </span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param <span class="type">&#123;*&#125;</span> </span>label </span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Button</span>(<span class="params">width,height,label</span>)</span>&#123;</span><br><span class="line">    Widget.call(<span class="built_in">this</span>,width,height);</span><br><span class="line">    <span class="built_in">this</span>.label = label;</span><br><span class="line">&#125;</span><br><span class="line">Button.prototype = <span class="built_in">Object</span>.create(Widget.prototype);</span><br><span class="line"><span class="comment">//重写render方法</span></span><br><span class="line">Button.prototype.render = <span class="function"><span class="keyword">function</span>(<span class="params">$where</span>)</span>&#123;</span><br><span class="line">    <span class="comment">//super调用</span></span><br><span class="line">    Widget.prototype.render.call(<span class="built_in">this</span>,$where);</span><br><span class="line">    <span class="comment">//绑定事件</span></span><br><span class="line">    <span class="built_in">this</span>.$elem.click(<span class="built_in">this</span>.onClick.bind(<span class="built_in">this</span>));</span><br><span class="line">&#125;</span><br><span class="line">Button.prototype.onClick = <span class="function"><span class="keyword">function</span>(<span class="params">evt</span>)</span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&#x27;Button&#x27;</span>+<span class="built_in">this</span>.label+<span class="string">&#x27; clicked!&#x27;</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">//调用</span></span><br><span class="line">$(<span class="built_in">document</span>).render(<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="keyword">var</span> $body = $(<span class="built_in">document</span>.body);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">var</span> saveBtn = <span class="keyword">new</span> Button(<span class="number">125</span>,<span class="number">30</span>,<span class="string">&#x27;暂存&#x27;</span>);</span><br><span class="line">    <span class="keyword">var</span> submitBtn = <span class="keyword">new</span> Button(<span class="number">125</span>,<span class="number">30</span>,<span class="string">&#x27;提交&#x27;</span>);</span><br><span class="line"></span><br><span class="line">    saveBtn.render($body);</span><br><span class="line">    submitBtn.render($body);</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure></li>
<li>用面向委托的代码来更简单的实现Widget/Button：<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Widget父类</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">var</span> Widget = &#123;</span><br><span class="line">    init: <span class="function"><span class="keyword">function</span> (<span class="params">width, height</span>) </span>&#123;</span><br><span class="line">        <span class="built_in">this</span>.width = width || <span class="number">50</span>;</span><br><span class="line">        <span class="built_in">this</span>.height = height || <span class="number">50</span>;</span><br><span class="line">        <span class="built_in">this</span>.$elem = <span class="literal">null</span>;</span><br><span class="line">    &#125;,</span><br><span class="line">    insert: <span class="function"><span class="keyword">function</span> (<span class="params">$where</span>) </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (<span class="built_in">this</span>.$elem) &#123;</span><br><span class="line">            <span class="built_in">this</span>.$elem.css(&#123;</span><br><span class="line">                width: <span class="built_in">this</span>.width + <span class="string">&#x27;px&#x27;</span>,</span><br><span class="line">                height: <span class="built_in">this</span>.height + <span class="string">&#x27;px&#x27;</span></span><br><span class="line">            &#125;).appendTo($where);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Button子类</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">var</span> Button = <span class="built_in">Object</span>.create(Widget);</span><br><span class="line">Button.setup = <span class="function"><span class="keyword">function</span>(<span class="params">width,height,label</span>)</span>&#123;</span><br><span class="line">    <span class="built_in">this</span>.init(width,height);</span><br><span class="line">    <span class="built_in">this</span>.label = label || <span class="string">&#x27;Default&#x27;</span>;</span><br><span class="line">    <span class="built_in">this</span>.$elem = $(<span class="string">&#x27;&lt;button&gt;&#x27;</span>).text(<span class="built_in">this</span>.label);</span><br><span class="line">&#125;</span><br><span class="line">Button.build = <span class="function"><span class="keyword">function</span>(<span class="params">$where</span>)</span>&#123;</span><br><span class="line">    <span class="built_in">this</span>.insert($where);</span><br><span class="line">    <span class="built_in">this</span>.$elem.click(<span class="built_in">this</span>.onClick.bind(<span class="built_in">this</span>));</span><br><span class="line">&#125;</span><br><span class="line">Button.onClick = <span class="function"><span class="keyword">function</span>(<span class="params">evt</span>)</span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&#x27;Button&#x27;</span>+<span class="built_in">this</span>.label+<span class="string">&#x27; clicked!&#x27;</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//调用</span></span><br><span class="line">$(<span class="built_in">document</span>).render(<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="keyword">var</span> $body = $(<span class="built_in">document</span>.body);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">var</span> saveBtn = <span class="built_in">Object</span>.create(Button);</span><br><span class="line">    saveBtn.setup(<span class="number">125</span>,<span class="number">30</span>,<span class="string">&#x27;暂存&#x27;</span>);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">var</span> submitBtn = <span class="built_in">Object</span>.create(Button);</span><br><span class="line">    submitBtn.setup(<span class="number">125</span>,<span class="number">30</span>,<span class="string">&#x27;提交&#x27;</span>);</span><br><span class="line"></span><br><span class="line">    saveBtn.build($body);</span><br><span class="line">    submitBtn.build($body);</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<blockquote>
<p>注意：</p>
<ul>
<li>在面向委托的代码中，没有像类一样，定义相同的方法名<code>render()</code>，而是定义了两个更具描述性的方法名<code>insert()</code>和<code>build()</code>，在很多情况下，将构造和初始化步骤分开，更灵活；</li>
<li>同时在面向委托的代码中，我们避免丑陋的显式伪多态调用<code>Widget.call</code>和<code>Widget.prototype.render.call</code>，取而代之简单的相对委托调用<code>this.init()</code>和<code>this.insert()</code>；</li>
</ul>
</blockquote>
</li>
</ul>
<h3 id="6-3-更简洁的设计"><a href="#6-3-更简洁的设计" class="headerlink" title="6.3 更简洁的设计"></a>6.3 更简洁的设计</h3><ul>
<li>第三个例子，是分别用“面向类”和“面向委托”的风格代码来实现一个登陆界面。<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 父类Controller</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Controller</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="built_in">this</span>.errors = [];</span><br><span class="line">&#125;</span><br><span class="line">Controller.prototype.showDialog = <span class="function"><span class="keyword">function</span>(<span class="params">title,msg</span>)</span>&#123;</span><br><span class="line">    <span class="comment">//...</span></span><br><span class="line">&#125;</span><br><span class="line">Controller.prototype.success = <span class="function"><span class="keyword">function</span>(<span class="params">msg</span>)</span>&#123;</span><br><span class="line">    <span class="built_in">this</span>.showDialog(<span class="string">&#x27;Success&#x27;</span>,msg);</span><br><span class="line">&#125;</span><br><span class="line">Controller.prototype.failure = <span class="function"><span class="keyword">function</span>(<span class="params">err</span>)</span>&#123;</span><br><span class="line">    <span class="built_in">this</span>.errors.push(err);</span><br><span class="line">    <span class="built_in">this</span>.showDialog(<span class="string">&#x27;Error&#x27;</span>,err);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 登陆表单Controller子类</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">LoginController</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    Controller.call(<span class="built_in">this</span>);</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//继承Controller</span></span><br><span class="line">LoginController.prototype = <span class="built_in">Object</span>.create(Controller.prototype);</span><br><span class="line"><span class="comment">//获取用户名</span></span><br><span class="line">LoginController.prototype.getUser = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="keyword">return</span> docuemnt.getElmementById(<span class="string">&#x27;username&#x27;</span>).value;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//获取密码</span></span><br><span class="line">LoginController.prototype.getPwd = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="keyword">return</span> docuemnt.getElmementById(<span class="string">&#x27;userPwd&#x27;</span>).value;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//表单校验</span></span><br><span class="line">LoginController.prototype.validate = <span class="function"><span class="keyword">function</span>(<span class="params">user,pwd</span>)</span>&#123;</span><br><span class="line">    user = user || <span class="built_in">this</span>.getUser();</span><br><span class="line">    pwd = pwd || <span class="built_in">this</span>.getPwd();</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span>(!(user &amp;&amp; pwd))&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">this</span>.failure(<span class="string">&#x27;请输入用户名/密码!&#x27;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">else</span> <span class="keyword">if</span>(pwd.length &lt; <span class="number">5</span>)&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">this</span>.failure(<span class="string">&#x27;密码长度不能少于五位!&#x27;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//重写failure</span></span><br><span class="line">LoginController.prototype.failure = <span class="function"><span class="keyword">function</span>(<span class="params">err</span>)</span>&#123;</span><br><span class="line">    Controller.prototype.failure.call(<span class="built_in">this</span>,<span class="string">&#x27;登陆失败：&#x27;</span>+err);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 授权检查Controller子类</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param <span class="type">&#123;*&#125;</span> </span>login </span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">AuthController</span>(<span class="params">login</span>)</span>&#123;</span><br><span class="line">    Controller.call(<span class="built_in">this</span>);</span><br><span class="line">    <span class="built_in">this</span>.login = login;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//继承Controller</span></span><br><span class="line">AuthController.prototype = <span class="built_in">Object</span>.create(Controller.prototype);</span><br><span class="line"><span class="comment">//授权检查</span></span><br><span class="line">AuthController.prototype.checkAuth = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="keyword">var</span> user = <span class="built_in">this</span>.login.getUser();</span><br><span class="line">    <span class="keyword">var</span> pwd = <span class="built_in">this</span>.login.getPwd();</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span>(<span class="built_in">this</span>.login.validate(user,pwd))&#123;</span><br><span class="line">        <span class="built_in">this</span>.send(<span class="string">&#x27;/check-auth&#x27;</span>,&#123;</span><br><span class="line">            user : user,</span><br><span class="line">            pwd : pwd</span><br><span class="line">        &#125;)</span><br><span class="line">        .then(<span class="built_in">this</span>.success.bind(<span class="built_in">this</span>))</span><br><span class="line">        .fail(<span class="built_in">this</span>.failure.bind(<span class="built_in">this</span>));</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//发送请求</span></span><br><span class="line">AuthController.prototype.send = <span class="function"><span class="title">functioin</span>(<span class="params">url,data</span>)</span>&#123;</span><br><span class="line">    <span class="keyword">return</span> $.ajax(&#123;</span><br><span class="line">        url : url,</span><br><span class="line">        data : data</span><br><span class="line">    &#125;)</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//重写基础的success</span></span><br><span class="line">AuthController.prototype.success = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    Controller.prototype.success.call(<span class="built_in">this</span>,<span class="string">&#x27;授权检查成功!&#x27;</span>);</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//重写基础的failure</span></span><br><span class="line">AuthController.prototype.failure = <span class="function"><span class="keyword">function</span>(<span class="params">err</span>)</span>&#123;</span><br><span class="line">    Controller.prototype.failure.call(<span class="built_in">this</span>,<span class="string">&#x27;授权检查失败：&#x27;</span>+err);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 调用</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="comment">//除了继承，还要合成</span></span><br><span class="line"><span class="keyword">var</span> auth = <span class="keyword">new</span> AuthController(<span class="keyword">new</span> LoginController());</span><br><span class="line">auth.checkAuth();</span><br></pre></td></tr></table></figure></li>
<li>用对象关联风格的行为委托来实现：<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Login</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">var</span> LoginController = &#123;</span><br><span class="line">    errors : [],</span><br><span class="line">    getUser : <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span> docuemnt.getElmementById(<span class="string">&#x27;username&#x27;</span>).value;</span><br><span class="line">    &#125;,</span><br><span class="line">    getPwd : <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span> docuemnt.getElmementById(<span class="string">&#x27;userPwd&#x27;</span>).value;</span><br><span class="line">    &#125;,</span><br><span class="line">    validate : <span class="function"><span class="keyword">function</span>(<span class="params">user,pwd</span>)</span>&#123;</span><br><span class="line">        user = user || <span class="built_in">this</span>.getUser();</span><br><span class="line">        pwd = pwd || <span class="built_in">this</span>.getPwd();</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span>(!(user &amp;&amp; pwd))&#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="built_in">this</span>.failure(<span class="string">&#x27;请输入用户名/密码!&#x27;</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">else</span> <span class="keyword">if</span>(pwd.length &lt; <span class="number">5</span>)&#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="built_in">this</span>.failure(<span class="string">&#x27;密码长度不能少于五位!&#x27;</span>);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">    &#125;,</span><br><span class="line">    showDialog : <span class="function"><span class="keyword">function</span>(<span class="params">title,msg</span>)</span>&#123;</span><br><span class="line">        <span class="comment">//...</span></span><br><span class="line">    &#125;,</span><br><span class="line">    failure : <span class="function"><span class="keyword">function</span>(<span class="params">err</span>)</span>&#123;</span><br><span class="line">        <span class="built_in">this</span>.errors.push(err);</span><br><span class="line">        <span class="built_in">this</span>.showDialog(<span class="string">&#x27;Error&#x27;</span>,<span class="string">&#x27;登陆失败：&#x27;</span>+err);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Auth</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">var</span> AuthController = <span class="built_in">Object</span>.create(LoginController);</span><br><span class="line">AuthController.errors = [];</span><br><span class="line">AuthController.checkAuth = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="keyword">var</span> user = <span class="built_in">this</span>.login.getUser();</span><br><span class="line">    <span class="keyword">var</span> pwd = <span class="built_in">this</span>.login.getPwd();</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span>(<span class="built_in">this</span>.login.validate(user,pwd))&#123;</span><br><span class="line">        <span class="built_in">this</span>.send(<span class="string">&#x27;/check-auth&#x27;</span>,&#123;</span><br><span class="line">            user : user,</span><br><span class="line">            pwd : pwd</span><br><span class="line">        &#125;)</span><br><span class="line">        .then(<span class="built_in">this</span>.success.bind(<span class="built_in">this</span>))</span><br><span class="line">        .fail(<span class="built_in">this</span>.failure.bind(<span class="built_in">this</span>));</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">AuthController.send = <span class="function"><span class="title">functioin</span>(<span class="params">url,data</span>)</span>&#123;</span><br><span class="line">    <span class="keyword">return</span> $.ajax(&#123;</span><br><span class="line">        url : url,</span><br><span class="line">        data : data</span><br><span class="line">    &#125;)</span><br><span class="line">&#125;</span><br><span class="line">AuthController.accepted = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="built_in">this</span>.showDialog(<span class="string">&#x27;系统提示&#x27;</span>,<span class="string">&#x27;授权检查成功!&#x27;</span>);</span><br><span class="line">&#125;</span><br><span class="line">AuthController.rejected = <span class="function"><span class="keyword">function</span>(<span class="params">err</span>)</span>&#123;</span><br><span class="line">    <span class="built_in">this</span>.showDialog(<span class="string">&#x27;系统提示&#x27;</span>,<span class="string">&#x27;授权检查失败：&#x27;</span>+err)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 调用</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">var</span> loginCtrl = <span class="built_in">Object</span>.create(LoginController);</span><br><span class="line"><span class="keyword">var</span> authCtrl = <span class="built_in">Object</span>.create(AuthController);</span><br></pre></td></tr></table></figure></li>
<li>在行为委托模式中，<code>AuthController</code>和<code>LoginController</code>只是兄弟关系的对象，不是子类和父类的关系。相比起面向类的设计模式，只需要两个实体就可以了，也不需要实例化类。</li>
</ul>
<h3 id="6-4-更好的语法"><a href="#6-4-更好的语法" class="headerlink" title="6.4 更好的语法"></a>6.4 更好的语法</h3><ul>
<li>另外，ES6的<code>class</code>的语法可以简洁的定义类方法，不用<code>function</code>关键字：<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> LoginController = &#123;</span><br><span class="line">    errors : [],</span><br><span class="line">    <span class="function"><span class="title">getUser</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">        <span class="comment">//可以不用function来定义</span></span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="function"><span class="title">getPwd</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">        <span class="comment">//...</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> AuthController = &#123;</span><br><span class="line">    errors : [],</span><br><span class="line">    <span class="function"><span class="title">checkAuth</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="comment">//...</span></span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="function"><span class="title">send</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="comment">//...</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//把AuthController关联到LoginController</span></span><br><span class="line"><span class="built_in">Object</span>.setPrototypeOf(AuthController,LoginController);</span><br></pre></td></tr></table></figure>

</li>
</ul>
<h3 id="6-5-内省"><a href="#6-5-内省" class="headerlink" title="6.5 内省"></a>6.5 内省</h3><ul>
<li>内省即检查实例的类型，在这个方面上，面向委托的代码也比面向类更方便。</li>
<li>前文提到，检测实例是否属于某个类对象，用<code>instance</code>：<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//类</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Fruit</span>(<span class="params"></span>)</span>&#123;&#125;</span><br><span class="line">Fruit.prototype.something = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;&#125;</span><br><span class="line"><span class="comment">//实例</span></span><br><span class="line"><span class="keyword">var</span> fruit = <span class="keyword">new</span> Fruit();</span><br><span class="line"><span class="comment">//判断实例的类型，以便调用某个方法</span></span><br><span class="line"><span class="keyword">if</span>(fruit <span class="keyword">instanceof</span> Fruit)&#123;</span><br><span class="line">    fruit.something();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li>但如果是多继承关系的情况下，则必须借助<code>Object.isPrototypeOf()</code>和<code>Object.getPrototypeOf（）</code>等方法：<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//水果</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Fruit</span>(<span class="params"></span>)</span>&#123;&#125;</span><br><span class="line">Fruit.prototype.something = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;&#125;</span><br><span class="line"><span class="comment">//苹果</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Apple</span>(<span class="params"></span>)</span>&#123;&#125;</span><br><span class="line">Apple.prototype = <span class="built_in">Object</span>.create(Fruit.prototype);</span><br><span class="line"><span class="comment">//苹果实例</span></span><br><span class="line"><span class="keyword">var</span> apple = <span class="keyword">new</span> Apple();</span><br><span class="line"></span><br><span class="line"><span class="comment">//检测结果均为true</span></span><br><span class="line"><span class="built_in">console</span>.log(Apple.prototype <span class="keyword">instanceof</span> Fruit);</span><br><span class="line"><span class="built_in">console</span>.log(<span class="built_in">Object</span>.getPrototypeOf(Apple.prototype) === Fruit.prototype);</span><br><span class="line"><span class="built_in">console</span>.log(Fruit.prototype.isPrototypeOf(Apple.prototype));</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(apple <span class="keyword">instanceof</span> Apple);</span><br><span class="line"><span class="built_in">console</span>.log(apple <span class="keyword">instanceof</span> Fruit);</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(<span class="built_in">Object</span>.getPrototypeOf(apple) === Apple.prototype);</span><br><span class="line"><span class="built_in">console</span>.log(Apple.prototype.isPrototypeOf(apple));</span><br><span class="line"><span class="built_in">console</span>.log(Fruit.prototype.isPrototypeOf(apple));</span><br><span class="line"></span><br><span class="line"><span class="comment">//或者通过鸭子类型的方式来检查，只要方法存在即调用</span></span><br><span class="line"><span class="keyword">if</span>(apple.something)&#123;</span><br><span class="line">    apple.something();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li>而面向委托的代码检测起来则比较简单：<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> Fruit = &#123;</span><br><span class="line">    something : <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;&#125;</span><br><span class="line">&#125;;</span><br><span class="line"><span class="keyword">var</span> Apple = <span class="built_in">Object</span>.create(Fruit);</span><br><span class="line"><span class="keyword">var</span> apple = <span class="built_in">Object</span>.craate(Apple);</span><br><span class="line"></span><br><span class="line"><span class="comment">//检测结果均为true</span></span><br><span class="line"><span class="built_in">console</span>.log(Fruit.isPrototypeOf(Apple));</span><br><span class="line"><span class="built_in">console</span>.log(<span class="built_in">Object</span>.getPrototypeOf(Apple) === Apple);</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(Fruit.isPrototypeOf(apple));</span><br><span class="line"><span class="built_in">console</span>.log(Apple.isPrototypeOf(apple));</span><br><span class="line"><span class="built_in">console</span>.log(<span class="built_in">Object</span>.getPrototypeOf(apple) === Apple);</span><br></pre></td></tr></table></figure>

</li>
</ul>
<h3 id="6-6-小结"><a href="#6-6-小结" class="headerlink" title="6.6 小结"></a>6.6 小结</h3><ul>
<li>JavaScript是一门灵活的语言，既可以采用面向类和继承的设计模式，这也是大多数开发者习惯的代码组织方式，另外也可以采用行为委托的设计模式。</li>
<li>相对于“面向类”的设计模式把对象看成父子关系，行为委托的设计模式认为对象之间是相互委托的兄弟关系。</li>
<li>JavaScript的<code>[[Prototype]]</code>机制本质就是行为委托机制，所以当使用面向委托的设计模式来组织代码时，也让代码更简洁和清晰。</li>
</ul>

    </div>

    
    
    

    <footer class="post-footer">
          

<div class="post-copyright">
<ul>
  <li class="post-copyright-author">
      <strong>本文作者： </strong>William Leung
  </li>
  <li class="post-copyright-link">
      <strong>本文链接：</strong>
      <a href="https://gitee.com/lianghuilin/murmur.git/reading_notes/%E3%80%8A%E4%BD%A0%E4%B8%8D%E7%9F%A5%E9%81%93%E7%9A%84JavaScript-%E4%B8%8A-%E3%80%8BS02E06/" title="《你不知道的JavaScript(上)》S02E06">https://gitee.com/lianghuilin/murmur.git/reading_notes/《你不知道的JavaScript-上-》S02E06/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" rel="noopener" target="_blank"><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处！
  </li>
</ul>
</div>

          <div class="post-tags">
              <a href="/murmur/tags/%E4%BD%A0%E4%B8%8D%E7%9F%A5%E9%81%93%E7%9A%84JavaScript/" rel="tag"># 你不知道的JavaScript</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/murmur/reading_notes/%E3%80%8A%E4%BD%A0%E4%B8%8D%E7%9F%A5%E9%81%93%E7%9A%84JavaScript-%E4%B8%8A-%E3%80%8BS02E05/" rel="prev" title="《你不知道的JavaScript(上)》S02E05">
                  <i class="fa fa-chevron-left"></i> 《你不知道的JavaScript(上)》S02E05
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/murmur/interview_questions/js/new%E4%B8%80%E4%B8%AAJS%E5%AF%B9%E8%B1%A1%E7%BB%8F%E5%8E%86%E4%BA%86%E4%BB%80%E4%B9%88/" rel="next" title="new一个JS对象经历了什么">
                  new一个JS对象经历了什么 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






      

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      const activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      const commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

    </div>
  </main>

  <footer class="footer">
    <div class="footer-inner">
      

      

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">William Leung</span>
</div>
<div class="wordcount">
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-line"></i>
    </span>
    <span title="站点总字数">142k</span>
  </span>
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
    <span title="站点阅读时长">2:09</span>
  </span>
</div>
<div class="busuanzi-count">
    <span id="busuanzi_container_site_uv">
      本站访客数<span id="busuanzi_value_site_uv"></span>人次
    </span>
    <span id="busuanzi_container_site_pv">
        本站总访问量<span id="busuanzi_value_site_pv"></span>次
    </span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/" class="theme-link" rel="noopener" target="_blank">NexT.Gemini</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <script src="//cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js"></script>
<script src="/murmur/js/utils.js"></script><script src="/murmur/js/motion.js"></script><script src="/murmur/js/next-boot.js"></script>

  
















  <script>
    NProgress.configure({
      showSpinner: true
    });
    NProgress.start();
    document.addEventListener('readystatechange', () => {
      if (document.readyState === 'interactive') {
        NProgress.inc(0.8);
      }
      if (document.readyState === 'complete') {
        NProgress.done();
      }
    });
    document.addEventListener('pjax:send', () => {
      NProgress.start();
    });
    document.addEventListener('pjax:success', () => {
      NProgress.done();
    });
  </script>


  
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>








  

  

</body>
</html>
